/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package home.sockets.view;

import home.sockets.controller.concurrent.Checker;
import home.sockets.controller.concurrent.CheckerListener;
import home.sockets.controller.parsers.Sock5Parser;
import home.sockets.controller.providers.ServersParser;
import home.sockets.controller.proxy.ProxyServer;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collection;
import java.util.ResourceBundle;

import home.sockets.controller.utils.SpringFactory;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.TextArea;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;
import javafx.scene.input.MouseEvent;

/**
 *
 * @author Evgeny
 */
public class SampleController implements Initializable
{
    @FXML
    private TextArea text_area;
    @FXML
    private TableView<ProxyServer> servers_table;
    @FXML
    private TableColumn<ProxyServer, String> ipcolumn;
    @FXML
    private TableColumn<ProxyServer, String> portcolumn;
    @FXML
    private TableColumn<ProxyServer, String> pingcolumn;

    private boolean isClear = true;
    @FXML
    private ObservableList<ProxyServer> data = null;

    /**
     * Initializes the controller class.
     */
    @FXML
    public void initialize(URL url, ResourceBundle rb)
    {

        ObservableList<ProxyServer> list = FXCollections.observableArrayList();
        data = FXCollections.synchronizedObservableList(list);
        registerTextAreaListener();
        ipcolumn.setCellValueFactory(new PropertyValueFactory("ip"));
        ipcolumn.setCellFactory(TextFieldTableCell.<ProxyServer>forTableColumn());

        portcolumn.setCellValueFactory(new PropertyValueFactory<ProxyServer, String>("port"));
        portcolumn.setCellFactory(TextFieldTableCell.<ProxyServer>forTableColumn());

        pingcolumn.setCellValueFactory(new PropertyValueFactory<ProxyServer, String>("ping"));
        servers_table.setItems(data);
    }    

    @FXML
    private void handleButtonAction(ActionEvent event) 
    {
        try
        {
            data.clear();
        }
        catch (Exception ex)
        {

        }
        Collection<String> serversAsStrings = parseText();
        ServersParser parser = new ServersParser();
        Checker checker = SpringFactory.XML_INSTANCE.getBean(Checker.class);
        try
        {
            Collection<ProxyServer> proxyServers = parser.getServersList(serversAsStrings);
            checker.check(proxyServers, new CheckerListener<ProxyServer>() 
            {

                @Override
                public void doAction(ProxyServer result)
                {
                    System.out.println("Server added " + result);
                    data.add(result);
                }
            });
        }
        catch (Exception ex)
        {
            System.out.println(ex);
            throw new RuntimeException(ex);
        }
    }


    
    private Collection<String> parseText()
    {
        Collection<String> servers = new ArrayList<>();
        String text = text_area.getText();
        //text_area.clear();
        if (text != null && !text.isEmpty())
        {
            String[] strings = text.split("\n");
            for (String str : strings)
            {
                servers.add(str);
            }
        }
        return servers;
    }

    @FXML
    private void handleTextAreaClick(MouseEvent event)
    {
        if (isClear)
        {
            text_area.clear();
            isClear = false;
        }
    }

    private void registerTextAreaListener()
    {
        text_area.focusedProperty().addListener(new ChangeListener<Boolean>()
        {
            @Override
            public void changed(ObservableValue<? extends Boolean> observableValue, Boolean aBoolean, Boolean aBoolean2)
            {
                if (aBoolean && !aBoolean2 && text_area.getText().isEmpty() && !isClear)
                {
                    text_area.setText("Input information about servers here.");
                    isClear = true;
                }
            }
        } );
    }


            
}
